package com.imodia.checkoo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.SqlParam;
import com.imodia.checkoo.bean.CmsDocumentBean;
import com.imodia.checkoo.bean.CmsModelBean;
import com.imodia.checkoo.service.CmsModelService;
import com.imodia.checkoo.service.impl.CmsDocumentServiceImpl;

/**
 *
 * @since 2001-12-18
 * @author lijun
 * @version 1.0
 * @Copyright ? 2009 LinkCm Co. Ltd. All right reserved.
 */

@Controller
@RequestMapping("/hfmanager/model/list.do")
public class CmsModelController {
	
	private final Log log = LogFactory.getLog(CmsModelController.class);
	
	private final String baseUrl = "model";
	
	private final String url_list = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list";
	private final String url_redirect = "/hfmanager/" +baseUrl + "/list.do?action=edit";
	private final String url_edit = "/hfmanager/" + baseUrl + "/"+baseUrl+"_edit";
	private final String url_list4article = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list_for_article";

	@Autowired
	private CmsModelService cmsModelService;
	@Autowired
	private CmsDocumentServiceImpl cmsDocumentService;
	
	@RequestMapping(params = "action=delete")
	public ModelAndView delete(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		cmsModelService.delete(id);
		int documentId = StringUtil.requestInt(request, "documentId");
		int refId = StringUtil.requestInt(request, "refId");
		String url = "/hfmanager/catalogue/list.do?action=edit&documentId="+documentId+"&id="+refId+"&x=1";
		return new ModelAndView(new RedirectView(request.getContextPath()+url), null);
	}

	@RequestMapping(params = "action=save")
	public ModelAndView edit(@ModelAttribute("userMessage")CmsModelBean obj, HttpServletRequest request) {
		int x = 0;
		if(obj.getId()!=null && obj.getId()>0){
			x = cmsModelService.update(obj);
		}else{
			x = cmsModelService.insert(obj);
			obj.setId(x);
		}
		if(obj.getIsDefault()==1){
			cmsModelService.updateDefault(obj);
		}
		if(x>0)x=1;
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect+"&id="+obj.getId()+"&x="+x), null);
	}
	
	@RequestMapping(params = "action=edit")
	public ModelAndView edit_in(HttpServletRequest request,CmsModelBean obj) {
		int id = StringUtil.requestInt(request,"id");
		Map<String,Object> m = new HashMap<String,Object>();
		m.put("info", cmsModelService.get(id));

		List<CmsDocumentBean> document = cmsDocumentService.list(new SqlParam());
		m.put("document", document);
		
	    return new ModelAndView(url_edit, m);
	}
	
	@RequestMapping(params = "action=list")
	public ModelAndView list(HttpServletRequest request, CmsModelBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		int documentId = StringUtil.requestInt(request, "documentId");
		SqlParam sql = new SqlParam();
		if(documentId>0){
			sql.andEqualTo("ref_id", String.valueOf(documentId));
		}
		sql.andEqualTo("ref_rs_id", String.valueOf(3));
		String cnName = request.getParameter("cnName");
		if(cnName!=null && cnName.trim().length()>0){
			sql.andLike("cn_name", "%"+cnName+"%");
		}

		PageResult<CmsModelBean> pageResult =cmsModelService.page(sql,page);
		model.put("page", pageResult);
		
		List<CmsDocumentBean> document = cmsDocumentService.list(new SqlParam());
		model.put("document", document);
		
		return new ModelAndView(url_list, model);
	}
	
	
	@RequestMapping(params = "action=list4document")
	public ModelAndView list4document(HttpServletRequest request, CmsModelBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		
		int documentId = StringUtil.requestInt(request, "documentId");
		SqlParam sql = new SqlParam();
		sql.andEqualTo("ref_id", String.valueOf(documentId));
		sql.andEqualTo("ref_rs_id", String.valueOf(3));
		PageResult<CmsModelBean> pageResult =cmsModelService.page(sql,page);
		model.put("page", pageResult);
		return new ModelAndView(url_list, model);
	}
	
//	@RequestMapping(params = "action=list4acticle")
//	public ModelAndView list4acticle(HttpServletRequest request, CmsModelBean obj, PageBean page) {
//		HashMap<String, Object> model = new HashMap<String, Object>();
//		cmsModelService.queryForPage(obj,model, new SqlParam(),page);
//		return new ModelAndView(url_list4acticle, model);
//	}
	
	@RequestMapping(params = "action=list4article")
	public ModelAndView list4acticle(HttpServletRequest request, CmsModelBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		
		int documentId = StringUtil.requestInt(request,"documentId");
		SqlParam sql = new SqlParam();
		sql.andEqualTo("ref_id", String.valueOf(documentId));
		sql.andEqualTo("ref_rs_id", "3");
		PageResult<CmsModelBean> pageResult =cmsModelService.page(sql,page);
		model.put("page", pageResult);
		return new ModelAndView(url_list4article, model);
	}
	
	
}
